package cn.dnui_cr503.dnuifood_16110100503.controller;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import java.util.List;
import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.adapter.FoodListAdapter;
import cn.dnui_cr503.dnuifood_16110100503.bean.FoodBean;
import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.model.CollectionModel;
import cn.dnui_cr503.dnuifood_16110100503.model.FoodModel;

public class FoodListActivity extends AppCompatActivity implements OnRetrofitListener{
    private RecyclerView recyclerView;
    private FoodListAdapter foodListAdapter;
    private List<FoodBean> foodlist;
    String shop_id;
    private Menu menu;

    public FoodListActivity(){

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_foodlist);

        shop_id = getIntent().getStringExtra("shop_id");

        initViews();
        initData();
    }

    void initViews(){
        recyclerView = (RecyclerView)findViewById(R.id.food_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    void initData(){
        getFoodList();
    }

    private void getFoodList(){
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodByShop(shop_id,this);//访问服务器请求
    }

    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag){
            case 1:
                foodlist=(List<FoodBean>) object;
                foodListAdapter = new FoodListAdapter(this, foodlist);
                recyclerView.setAdapter(foodListAdapter);
                break;
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(FoodListActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.menu_collect,menu);
        this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
//        menu.findItem(R.id.item_collect).setTitle(shopname);
        isCollected(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //实现"显示收藏还是取消收藏"
        switch (item.getItemId()){
            case R.id.item_collect:
                String title = item.getTitle().toString();
                if (title.equals("收藏"))
                {
                    //收藏文章
                    collectShop(menu);
                }
                else
                {
                    //取消收藏
                    uncollectShop(menu);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void isCollected(Menu menu){
        String shop_id = getIntent().getStringExtra("shop_id");
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");

        CollectionModel collectModel = new CollectionModel();
        collectModel.isCollect(user_id,shop_id,"0",menu);
    }

    private void collectShop(Menu menu){
        String shop_id = getIntent().getStringExtra("shop_id");
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectionModel collectModel = new CollectionModel();
        collectModel.userCollectShop(user_id,shop_id,menu);

    }

    private void uncollectShop(Menu menu){
        String shop_id = getIntent().getStringExtra("shop_id");
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectionModel collectModel = new CollectionModel();
        collectModel.unCollectShop(user_id,shop_id,menu);
    }

}
